async function 并发请求() {
    const 链接数组 = [
        'http://117.35.25.150:8000/cms/business/contract/item?id=1',
        'http://117.35.25.150:8000/cms/business/contract/item?id=2',
    ];

    try {
        const 响应数组 = await Promise.all(链接数组.map(链接 => fetch(链接)));
        const 数据数组 = await Promise.all(响应数组.map(响应 => 响应.json()));
        console.log(数据数组);
    }
    catch (错误) {
        console.error(`请求失败: ${错误.message}`);
    }
}

await 并发请求();

console.log('---------------------------------');

async function 并发任务() {
    const 任务1 = new Promise(成功 => setTimeout(() => 成功('任务1完成'), 1000));
    const 任务2 = new Promise(成功 => setTimeout(() => 成功('任务2完成'), 500));

    try {
        const [结果1, 结果2] = await Promise.all([任务1, 任务2]);
        console.log(结果1, 结果2);
    }
    catch (错误) {
        console.log('任务出错: ', 错误.message);
    }
}
await 并发任务();

console.log('---------------------------------');

async function 部分请求失败() {
    const 任务数组 = [
        fetch('http://117.35.25.150:8000/cms/business/contract/item?id=1'),
        fetch('http://117.35.25.150:8000/cms/business/contract/item?id=2'),
    ];

    const 响应数组 = await Promise.allSettled(任务数组);
    for (const [索引, 响应] of 响应数组.entries()) {
        if (响应.status === 'fulfilled') {
            const 数据 = await 响应.value.json();
            console.log(`请求 ${索引} 成功: ${数据}`);
        }
        else {
            console.log(`请求 ${索引} 失败: ${响应.reason.message}`)
        }
    }

}
await 部分请求失败();

console.log('-------------------------------');

async function 限制并发数(任务数组, 并发数) {
    const 结果数组 = [];
    const 执行集合 = new Set();

    for(const 任务 of 任务数组) {
        const 期约 = 任务();
        执行集合.add(期约);

        (async () => {
            try {
                const 结果 = await 期约;
                结果数组.push(结果);
            }
            finally {
                执行集合.delete(期约);
            }
        })();

        while (执行集合.size >= 并发数) {
            await Promise.race([...执行集合]);
        }
    }

    await Promise.all([...执行集合]);
    return 结果数组;
}

const 任务数组 = new Array(5).fill(
    () => fetch('http://117.35.25.150:8000/cms/business/contract/item?id=1')
);
console.log(await 限制并发数(任务数组, 2));
console.log('所有任务完成');